'use strict';

module.exports = app => {
  const { STRING, INTEGER, TEXT, BOOLEAN, DATE, ENUM } = app.Sequelize;

  const Share = app.model.define('share', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    user_id: {
      type: INTEGER,
      allowNull: false,
      comment: '用户ID',
    },
    target_type: {
      type: ENUM('outfit', 'clothing'),
      allowNull: false,
      comment: '分享目标类型',
    },
    target_id: {
      type: INTEGER,
      allowNull: false,
      comment: '目标ID',
    },
    platform: {
      type: STRING(50),
      comment: '分享平台',
    },
    caption: {
      type: TEXT,
      comment: '分享文案',
    },
    is_public: {
      type: BOOLEAN,
      defaultValue: true,
      comment: '是否公开',
    },
    view_count: {
      type: INTEGER,
      defaultValue: 0,
      comment: '查看次数',
    },
    like_count: {
      type: INTEGER,
      defaultValue: 0,
      comment: '点赞数',
    },
    comment_count: {
      type: INTEGER,
      defaultValue: 0,
      comment: '评论数',
    },
    share_count: {
      type: INTEGER,
      defaultValue: 0,
      comment: '转发数',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
    updated_at: {
      type: DATE,
      allowNull: false,
      defaultValue: app.Sequelize.NOW,
    },
    // deleted_at: {
    //   type: DATE,
    //   allowNull: true,
    //   comment: '软删除时间',
    // },
  }, {
    tableName: 'shares',
    timestamps: true,
    underscored: true,
    paranoid: false, // 暂时禁用软删除
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    // deletedAt: 'deleted_at',
    comment: '分享记录表',
  });

  // 关联关系
  Share.associate = function() {
    // 属于用户
    app.model.Share.belongsTo(app.model.User, {
      foreignKey: 'user_id',
      as: 'user',
    });

    // 关联搭配 - 需要在查询时手动添加 target_type 条件
    app.model.Share.belongsTo(app.model.Outfit, {
      foreignKey: 'target_id',
      as: 'outfit',
      constraints: false,
    });

    // 关联衣服 - 需要在查询时手动添加 target_type 条件
    app.model.Share.belongsTo(app.model.Clothing, {
      foreignKey: 'target_id',
      as: 'clothing',
      constraints: false,
    });
  };

  return Share;
};
